package cn.iocoder.yudao.module.system.service.a_pictures;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharPool;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.a_pictures.vo.FileBaseVO;
import cn.iocoder.yudao.module.system.controller.admin.a_pictures.vo.ImageRespVO;
import cn.iocoder.yudao.module.system.enums.FileType;
import cn.iocoder.yudao.module.system.factory.FileStorageFactory;
import cn.iocoder.yudao.module.system.service.file_storage.UploadService;
import cn.iocoder.yudao.module.system.util.a_gaojian.FileUtil;
import cn.iocoder.yudao.module.system.util.a_gaojian.ImageUtils;
import jodd.net.URLDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * @author zyk
 */
@Component
@Slf4j
public class ImageUploadServiceImpl implements UploadService {


    @Value("${file.storage}")
    private String FILE_STORAGE;

    @Value("${file.image.host}")
    private String imageHost;

    @Value("${file.image.path}")
    private String imagePath;

    @Resource
    FileStorageFactory factory;

    public String upload(MultipartFile file, String path, String host) {
        ImageRespVO imageRespVO = BeanUtil.copyProperties(factory.get(FILE_STORAGE).storage(file, path), ImageRespVO.class);
        return host + "/" + imageRespVO.getRelativePath();
    }

    @Override
    public ImageRespVO upload(MultipartFile file) {
        String fileName = URLDecoder.decode(file.getOriginalFilename(), "UTF-8");
        Long size = file.getSize();
        String ext = FileUtil.getExt(fileName);
        log.info("Image upload name:" + fileName + ",size:" + size);
        check(ext, size);
        ImageRespVO imageRespVO = BeanUtil.copyProperties(factory.get(FILE_STORAGE).storage(file, imagePath), ImageRespVO.class);
        imageRespVO.setUrl(imageHost + "/" + imageRespVO.getRelativePath());
        imageRespVO.setSize(ImageUtils.readImage(imageRespVO.getFile()));
        return imageRespVO;
    }

    @Override
    public void check(String ext, Long size) {
        String picFormats = "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd";
        String picMaxSize = "200MB";
        if (org.apache.commons.lang3.StringUtils.isNotBlank(picFormats)) {
            if (!Arrays.stream(picFormats.split(String.valueOf(CharPool.COMMA))).anyMatch(s -> s.equalsIgnoreCase(ext))) {
                throw new ServiceException(new ErrorCode(500, String.format("不支持%s图片格式上传!", ext)));
            }
        } else {
            throw new ServiceException(new ErrorCode(500, "请配置图片上传格式"));
        }
        if (org.apache.commons.lang3.StringUtils.isBlank(picMaxSize)) {
            picMaxSize = "10MB";
        }
        Long maxSize = Long.valueOf(picMaxSize.replace("MB", "").replace("mb", "").replace("m", ""));
        if (size > maxSize * 1024 * 1024) {
            throw new ServiceException(new ErrorCode(500, String.format("图片大小不能超过%s!", picMaxSize)));
        }
    }

    @Override
    public FileBaseVO merge(String uuid, int chunkTotal, Long size, String ext) {
        return null;
    }

    @Override
    public boolean accept(String s) {
        return s.equals(FileType.image.getCode().toString());
    }

}
